#ifndef MM_APPLICATION_H
#define MM_APPLICATION_H
#undef signals
extern "C"
{
    #include <gio/gio.h>
    #include <messaging-menu/messaging-menu-app.h>
}
#define signals public
#include <QObject>
#include <QSharedPointer>
#include "app/Utils/Helpers.h"
#include "Message.h"

namespace MessagingMenu
{
class Message;
class Source;
class Application : public QObject
{
    Q_OBJECT
    QML_WRITABLE_PROPERTY(QString, desktopId)
    QML_WRITABLE_PROPERTY(QString, errorString)
    Q_ENUMS(Error)
public:
    explicit Application(QObject *parent = 0);
    Application(QObject *parent, const QString &desktopId);

    enum Error {
        NO_DESKTOP_ID,
        FAILED_REGISTRATION,
    };
    static void sourceActivated(MessagingMenuApp *, const gchar *id, gpointer appPtr);
    void shiftPositions(const int &position, const int shift = 1);
    Source *createMessageSource(const QString sourceId, const QString &sourceLabel, const QString &icon, bool persistent, qint32 position);
    void removeSource(const Source &source);
    bool hasSource(const Source &source);

    MessagingMenuApp *getApp() const;

Q_SIGNALS:
    void error(Error error);
    // Emitted when src was activated in the messaging menu
    void msgSourceActivated(const QString &id);

public Q_SLOTS:

    void registerApplication();
    void unregisterApplication();

private Q_SLOTS:
    void initApplication();

private:
    MessagingMenuApp *m_app;
    QHash<QString, QSharedPointer<MessagingMenu::Source> > m_sources;
    void emitSourceActivated(const QString &id);
    QString errorToString(const Error error) {
        switch(error) {
        case NO_DESKTOP_ID:
            return QStringLiteral("Missing desktop id");
        case FAILED_REGISTRATION:
            return QStringLiteral("Failed to register application");
        default:
            return QStringLiteral("Unknown error :-/");
        }
    }

    bool checkDesktopIdExists();
};
}
#endif // MM_APPLICATION_H
